package flirtme.managers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.XMPPConnection;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.android.gcm.GCMRegistrar;

import flirtme.constants.Constants;
import flirtme.model.BroadcastMessage;
import flirtme.model.ContactModel;
import flirtme.model.CurrentUser;
import flirtme.model.Message;

public class UserManager    {

	private static UserManager instance = null;
	private CurrentUser user = null;
	private Boolean rememberUser = false;
	private boolean profileCreated = false;
	private XMPPConnection connection;
	private Map<String, ContactModel> contacts = new HashMap<String, ContactModel>();
	private Map<String, BroadcastMessage> broadcastMessages = new HashMap<String, BroadcastMessage>();
	private Boolean showBroadcastMessages = false;
	private boolean profileLoaded = false;

	public boolean getProfileCreated() {
		return profileCreated;
	}
	
	public void createHash(ArrayList<ContactModel> contactsArray) {
		contacts.clear();
		
		for (ContactModel contact : contactsArray) {
			contacts.put(contact.getUsername(),  contact);
		}
	}
	
	public ContactModel getContact (String username) {
		return contacts.get(username);		
	}

	public void setProfileCreated(boolean profileCreated) {
		this.profileCreated = profileCreated;
	}

	public static UserManager instance()
	{
		if(instance == null) {
			instance = new UserManager();
			instance.showBroadcastMessages = false;
		}
		return instance;
	}
	
	private UserManager() {
		//this.loadUserPreferences();
	}	
	
	public void loadUserPreferences(Context _context) {
		if (_context != null) {
			SharedPreferences sharedPref = _context.getSharedPreferences(Constants.APP_NAME_KEY, Context.MODE_PRIVATE);
			this.rememberUser = sharedPref.getBoolean(Constants.REMEMBER_USER_KEY, false);
			this.profileCreated = sharedPref.getBoolean(Constants.PROFILE_CREATED_KEY, false);
			instance.user = new CurrentUser(sharedPref);
		}
	}
	
	public void saveCurrentUserPreferences(Context _context) {
		if (_context != null) {
			SharedPreferences sharedPref = _context.getSharedPreferences(Constants.APP_NAME_KEY, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = sharedPref.edit();
			instance.user.updateSharedPreferences(editor);
			editor.putBoolean(Constants.REMEMBER_USER_KEY, rememberUser);
			editor.putBoolean(Constants.PROFILE_CREATED_KEY, profileCreated);
			editor.putBoolean(Constants.PROFILE_SYNCHRONIZED_KEY, false);
			editor.commit();
		}
	}
	
	public void logout(Context _context) {
		SharedPreferences settings = _context.getSharedPreferences(Constants.APP_NAME_KEY, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		instance.user.removeSharedPreferences(editor);
		editor.commit();
		if (connection != null) {
			if (connection.isConnected()) {
				instance.connection.disconnect();
			}
		}
		instance.contacts.clear();
		instance.broadcastMessages.clear();
		instance.user = null;
		instance.profileLoaded = false;
		GCMRegistrar.setRegisteredOnServer(_context, false);
		GCMRegistrar.unregister(_context);
				
	}
	
	public void setShouldShowBroadcastMessages(Boolean _showBroadcast) {
			instance.showBroadcastMessages = _showBroadcast;
	}
	
	public Boolean shouldShowBroadcastMessages () {
		return instance.showBroadcastMessages;
	}
	
//	public void createUser(String username, String userID, String password) {
//		instance.user = new CurrentUser(username, userID, password);
//	}
	
	public void setRememberSession (Boolean remember) {
		this.rememberUser = remember;
	}
	
	public Boolean getRememberSession() {
		return rememberUser;
	}
	
	public XMPPConnection getConnection() {
		return instance.connection;
	}

	public  Boolean isConnectedToChat ( ) {
		return connection != null &&  connection.isConnected();
	}
	
	public void setConnection(XMPPConnection _connection) {
		instance.connection = _connection;
	}

	public void setContacs(ArrayList<ContactModel> contactsArray) {
		instance.createHash(contactsArray);
	}

	public void createUser(HashMap<String, String> userMap) {
		CurrentUser user = new CurrentUser();
		user.setPassword(userMap.get(Constants.PASSWORD_KEY));
		user.setEmail(userMap.get(Constants.EMAIL_KEY));
		user.setUsername(userMap.get(Constants.USER_NAME));
		instance.user  = user;
	}

	public CurrentUser getUser() {
		return instance.user;
	}
	
	public void updateUserData(String userName, String firstName, String lastName, String email, String gender, String birthdate) {
		this.user.setUsername(userName);
		getUser().setFirstName(firstName);
		getUser().setLastName(lastName);
//		this.user.setAge(age);
		this.user.setBirthDay(birthdate);
		this.user.setEmail(email);
		this.user.setGender(gender);
	}
	
	public void setImageUrl(String url)
	{
		this.user.setAvatarUrl(url);
	}

	public HashMap<String, BroadcastMessage> getBroadcastMessages() {
		return (HashMap<String, BroadcastMessage>) broadcastMessages;
	}

	public void setBroadcastMessages(List<BroadcastMessage> p_broadcastMessages) {
		this.broadcastMessages.clear();
		BroadcastMessage message;
		for( int i = 0; i < p_broadcastMessages.size(); i++)
		{
			message = p_broadcastMessages.get(i);
			this.broadcastMessages.put( Long.toString(message.getId()), message);
		}
	}
	
	public BroadcastMessage getBroadCastMessageById(Long id) {
		if (id != null && this.broadcastMessages != null) {
			BroadcastMessage msg = this.broadcastMessages.get(Long.toString(id));
			return msg;
		}
		return null;
	}
	
	public boolean isProfileLoaded() {
		return profileLoaded;
	}

	public void setProfileLoaded(boolean profileLoaded) {
		this.profileLoaded = profileLoaded;
	}
}
